<template>
    <div class="mask">
        <div class="TMB-warp">
            <div class="titler">
                <span class="font-w fs16" style="font-weight: 700;"></span>
                <i class="el-icon-close" @click="CloseTables"></i>
            </div>
            <div class="middle">
                <ux-grid class="ux-table" ref="singleTable" :data="prposTabelData" :header-cell-style="{
                    color: 'rgba(0, 0, 0, 0.85)',
                    fontSize: '14px',
                    textAlign: 'left',
                    fontWeight: 'bold',
                    padding: 0,
                    height: '32px',
                    background: 'rgb(244,245,246)'
                }" :cell-style="{
                    color: 'rgba(96,98,102,1)',
                    fontSize: '14px',
                    textAlign: 'left',
                    padding: 0,
                    height: '32px',
                }" :checkbox-config="{
                    showHeader: false,
                }" highlight-current-row @current-change="handleCurrentChange" v-loading="loading"
                    element-loading-spinner="el-icon-loading" element-loading-background="#f8f8f8"
                    :element-loading-text="$t('message.bhView.loading')" height="500">
                    <template slot="empty"> No data </template>
                    <ux-table-column property="label" title="グループNo." width="130px">
                        <template slot-scope="scope">
                            <el-input class="borderNone right" v-model="scope.row.pos_ordr"
                                @input="scope.row.pos_ordr = Number($setMyNum(scope.row.pos_ordr, '3 - 0'))"></el-input>
                        </template>
                    </ux-table-column>
                    <ux-table-column property="label" title="グループ名称">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.pos_name" class="borderNone"
                                @input="scope.row.pos_name = $setMaxLength(scope.row.pos_name, 20)"></el-input>
                        </template>
                    </ux-table-column>
                </ux-grid>
                <div class="inputWarp">
                    <div style="padding: 7px 0 7px 0;">グループ名追加</div>
                    <el-input v-model="value" @input="value = $setMaxLength(value, 20)"></el-input>
                </div>
                <div class="footer">
                    <div>
                        <el-button type="default" size="mini" @click="addFn">追加</el-button>
                        <el-button type="default" size="mini" @click="delFn">削除</el-button>
                        <el-button type="default" size="mini" @click="saveFn">保存</el-button>
                    </div>
                    <el-button type="primary" size="mini" @click="CloseTables">閉じる</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BASE from '@/utils/base'
import { GetThickGroupViewInf, AddThickGroupViewInf, DeleteThickGroupViewInfVerify, DeleteThickGroupViewInf, UpdateThickGroupViewInf } from '@/api/api'
import eventBus from "@/eventBus/bus";
import { getLocalStorage } from "@/utils/storage";
import _ from 'lodash-es';
export default {
    name: "RightTabelShow",
    props: {
        prposTabelData1: Array,
        ttgPosOrdr: Number,
    },
    data() {
        return {
            prposTabelData: [],
            prposTabelDataClone: [],
            loading: false,
            value: '',
            selectItem: {}

        };
    },
    components: {},
    watch: {},
    methods: {
        handleCurrentChange(val) {
            console.log(val);
            this.selectItem = val
        },
        CloseTables() {
            this.$emit('hidden')
        },
        async saveFn() {
            console.log(this.prposTabelDataClone, this.prposTabelData);
            // let filterArr = this.prposTabelData.filter(item =>
            //     !this.prposTabelDataClone.some(obj => {
            //         return item.pos_ordr == obj.pos_ordr
            //     })
            // )
            // if (filterArr.length == 1) {
            //     this.$emit('changettgPosOrdr', filterArr[0].pos_ordr)
            // }
            let D = {
                DmgCode: this.$store.state.macCode,
                Account: getLocalStorage("userName"),
                dataList: this.prposTabelData
            };
            let R = await UpdateThickGroupViewInf(D);
            if (R.data.code != 0) {
                this.$alert(R.data.msg, '', {
                    confirmButtonText: 'OK',
                    type: 'warning',
                });

                return
            }
            this.$message({
                type: 'success',
                message: 'OK'
            })
            this.$emit('hidden')
        },

        async delFn() {
            if (Object.keys(this.selectItem).length == 0) {
                return
            }
            let D = {
                DmgCode: this.$store.state.macCode,
                posOrdr: Number(this.selectItem.pos_ordr),
                grnPosNo: Number(this.selectItem.grn_pos_no),
            };
            let R = await DeleteThickGroupViewInfVerify(D);
            if (!R) {
                return
            }
            if (R.data.code == 451) {
                this.$confirm(R.data.msg, '', {
                    confirmButtonText: 'OK',
                    cancelButtonText: 'Cancel',
                    type: 'warning'
                }).then(async () => {
                    let R1 = await DeleteThickGroupViewInf(D);
                    if (R1.data.code == 0) {
                        this.selectItem = {};
                        this.$message({
                            type: 'success',
                            message: 'OK'
                        })
                        this.initFn();
                    }
                }).catch(() => {
                });
            }


        },
        async addFn() {

            if (this.value == '') {
                return
            }
            let D = {
                Account: getLocalStorage("userName"),
                DmgCode: this.$store.state.macCode,
                posName: this.value
            };
            let R = await AddThickGroupViewInf(D);
            if (R.data.code == 0) {
                this.value = ''
            }
            await this.initFn();
        },
        async initFn() {
            let dataJson = {
                DmgCode: this.$store.state.macCode,
            }
            let res = await GetThickGroupViewInf(dataJson);
            this.loading = false;
            if (res.data.code != 0) {
                return
            }
            this.prposTabelData = res.data.data;
            this.prposTabelDataClone = _.cloneDeep(this.prposTabelData);
        }
    },
    created() { },
    mounted() {
        this.initFn();
    },
    beforeDestroy() { },
    destroyed() { }
};
</script>
<style lang="scss" scoped>
.mask {
    // width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 500;
}

.TMB-warp {
    width: 700px;
    background: rgb(255, 255, 255);
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    box-shadow: 0px 12px 48px 16px rgba(0, 0, 0, 0.05),
        0px 9px 28px 0px rgba(0, 0, 0, 0.08), 0px 6px 16px -8px rgba(0, 0, 0, 0.15);
    z-index: 100;

    .titler {
        height: 43px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 13px 24px;
        border-bottom: 1px solid #F0F0F0FF;

        i {
            cursor: pointer;
        }
    }

    .middle {
        padding: 13px 24px;

        .title {
            display: flex;
            margin-bottom: 13px;

            .seach {
                width: 24px;
                height: 24px;
                border-radius: 4px;
                border: 1px solid #d9d9d9;
                display: flex;
                align-items: center;
                justify-content: center;
                cursor: pointer;
                margin-right: 12px;
            }

            .form-ipt {
                height: 24px;

                ::v-deep .el-input__inner {
                    width: 197px;
                    height: 24px;
                    background: #fff;
                }
            }
        }

        ::v-deep .ux-table * {
            border: none;
        }

        ::v-deep .elx-table {
            border: 1px solid #00000026;
        }

        .table {
            border-bottom: 1px solid red !important;
        }

        .inputWarp {
            border: 1px solid #00000026;
            padding: 5px;

            ::v-deep .el-input__inner {
                width: 100%;
                height: 31px;
                background: #fff;
            }
        }

        ;

        .footer {
            height: 43px;
            // border: 1px solid #00000026;
            border-top: none;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 4px;
        }
    }
}

.borderNone {
    ::v-deep .el-input__inner {
        background: none;
        border: 0 none;
        padding: 0;
    }
}

.right {
    ::v-deep .el-input__inner {
        text-align: right;
        padding-right: 15px;
    }
}
</style>
